package com.modbus.tcp.controller;

import com.alibaba.fastjson.JSONObject;
import com.modbus.tcp.entity.Tb3MysqlMybatisTest;
import com.modbus.tcp.service.Tb3MysqlMybatisTestImpl;
import com.modbus.tcp.mapper.Tb3MysqlMybatisTestMapper;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassDescription: 操作数据库的类
 * @Author：李白
 * @Date：2023/4/10 12:40
 */
@RestController
@RequestMapping("crud")
public class CrudController {

    @Resource
    Tb3MysqlMybatisTestMapper crudMapper;

    /**
     * 新增数据
     * @param testInfo
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSONObject create(@RequestBody Tb3MysqlMybatisTest testInfo){

        crudMapper.insert(testInfo);

        JSONObject insertObj = new JSONObject();
        insertObj.put("insertInfo",testInfo);
        return insertObj;

    }

//    /**
//     * 查询数据
//     * @return
//     */
//    @GetMapping(value = "read")
//    @Scheduled(fixedDelay = 5000)
//    public JSONObject read() throws InterruptedException {
//
//        Thread.sleep(10000);
//        Tb3MysqlMybatisTestImpl testImpl = new Tb3MysqlMybatisTestImpl();
//        List<Tb3MysqlMybatisTest> testList = crudMapper.selectByExample(testImpl);
//
//        JSONObject checkObj = new JSONObject();
//        checkObj.put("checkObj",testList);
//
//        Date date  = new Date();
//        SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String format = dateFormat.format(date);
//        System.out.println("crud系统时间"+format );
//        return checkObj;
//
//    }

    /**
     * 更新数据
     * @param testInfo
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSONObject update(@RequestBody Tb3MysqlMybatisTest testInfo){

        Tb3MysqlMybatisTestImpl testImpl = new Tb3MysqlMybatisTestImpl();
        Tb3MysqlMybatisTestImpl.Criteria criteria = testImpl.createCriteria();
        criteria.andUserIdEqualTo(testInfo.getUserId());
        crudMapper.updateByExampleSelective(testInfo, testImpl);

        JSONObject updateObj = new JSONObject();
        updateObj.put("updateObj", testInfo);
        return updateObj;

    }

    /**
     * 删除数据
     * @param testInfo
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSONObject delete(@RequestBody Tb3MysqlMybatisTest testInfo){

        Tb3MysqlMybatisTestImpl testImpl = new Tb3MysqlMybatisTestImpl();
        Tb3MysqlMybatisTestImpl.Criteria criteria = testImpl.createCriteria();
        criteria.andUserIdEqualTo(testInfo.getUserId());
        crudMapper.deleteByExample(testImpl);

        JSONObject deleteObj = new JSONObject();
        deleteObj.put("deleteObj",testInfo);
        return deleteObj;

    }

}

